package com.situ.dao;

import com.situ.dto.PageEntity;
import com.situ.entity.Department;
import com.situ.util.DataSourceUtil;
import com.situ.util.StringUtil;

import java.sql.SQLException;
import java.util.List;

public class DepartmentDao {
    public List<Department> selectList(String name, PageEntity page) {
        String sql = "select * from department ";
        if (StringUtil.isNotBlank(name)) {
            sql += " where instr(name,'" + name + "')>0";
        }
        sql += " limit ?,?";
        try {
            return DataSourceUtil.executeQuery(sql, Department.class, (page.getPage() - 1) * page.getLimit(), page.getLimit());
        } catch (SQLException | InstantiationException | IllegalAccessException | NoSuchFieldException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public Integer count(String name) {
        String sql = "select count(1) from department ";
        if (StringUtil.isNotBlank(name)) {
            sql += " where instr(name,'" + name + "')>0";
        }
        try {
            return DataSourceUtil.executeCount(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public void update(Department dept) {
        String sql = "update department set name=?,tel=?,address=? where id = ?";
        try {
            DataSourceUtil.executeUpdate(sql, dept.getName(), dept.getTel(), dept.getAddress(), dept.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void add(Department dept) {
        String sql = "insert into department(name,tel,address) values(?,?,?)";
        try {
            DataSourceUtil.executeUpdate(sql, dept.getName(), dept.getTel(), dept.getAddress());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public List<Department> selectById(Integer id) {
        String sql = "select * from department where id = ?";
        try {
            return DataSourceUtil.executeQuery(sql, Department.class, id);
        } catch (SQLException | InstantiationException | IllegalAccessException | NoSuchFieldException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public void delete(Integer id) {
        String sql = "delete from department where id = ?";
        try {
            DataSourceUtil.executeUpdate(sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public List<Department> selectAll() {
        String sql = "select * from department";
        try {
            return DataSourceUtil.executeQuery(sql,Department.class);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }
}
